package hzn.service.impl;

import hzn.dao.UserDao;
import hzn.pojo.User;
import hzn.service.UserService;
import hzn.util.PassUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.ReactiveTransaction;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void regist(User u) {
        String salt= PassUtil.randomSalt();//盐值
        u.setPass(PassUtil.encode(u.getPass(),salt));
        u.setSalt(salt);
        userDao.insert(u);
    }

    @Override
    public String login(User u) {
        UsernamePasswordToken tok=new UsernamePasswordToken(u.getName(),u.getPass());
        Subject sub=SecurityUtils.getSubject();
        String err=null;
        try{
            sub.login(tok);
        }catch ( Exception e){
            err=e.getMessage();
        }
        return err;
    }
}
